﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class NhaCungCapDAO
    {
        public List<NhaCungCapDTO> LayDanhSachNhaCungCap()
        {
            List<NhaCungCapDTO> ds = new List<NhaCungCapDTO>();
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_LayDanhSachNhaCungCap";
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                NhaCungCapDTO dto = new NhaCungCapDTO();
                dto.MaNhaCungCap = dr.GetInt32(0);
                dto.TenNhaCungCap = dr.GetString(1);
                dto.SoDienThoai = dr.GetString(2);
                dto.DiaChi = dr.GetString(3);
                dto.Email = dr.GetString(4);
                ds.Add(dto);
            }
            dr.Close();
            conn.Close();
            return ds;
        }

        public NhaCungCapDTO LayThongTinNhaCungCap(int ma)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_LayThongTinNhaCungCap";
            cm.Parameters.Add("@MaNhaCungCap", SqlDbType.Int).Value = ma;
            SqlDataReader dr = cm.ExecuteReader();
            NhaCungCapDTO dto = new NhaCungCapDTO();
            dr.Read();
            dto.MaNhaCungCap = dr.GetInt32(0);
            dto.TenNhaCungCap = dr.GetString(1);
            dto.SoDienThoai = dr.GetString(2);
            dto.DiaChi = dr.GetString(3);
            dto.Email = dr.GetString(4);
            dr.Close();
            conn.Close();
            return dto;
        }

        public string LayTenNhaCungCapTheoMa(int ma)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_LayTenNhaCungCapTheoMa";
            cm.Parameters.Add("@MaNhaCungCap", SqlDbType.Int).Value = ma;
            string ten = cm.ExecuteScalar().ToString();
            conn.Close();
            return ten;
        }

        public void ThemNhaCungCap(NhaCungCapDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_ThemNhaCungCap";
            cm.Parameters.Add("@TenNhaCungCap", SqlDbType.NVarChar).Value = dto.TenNhaCungCap;
            cm.Parameters.Add("@SoDienThoai", SqlDbType.VarChar).Value = dto.SoDienThoai;
            cm.Parameters.Add("@DiaChi", SqlDbType.NVarChar).Value = dto.DiaChi;
            cm.Parameters.Add("@Email", SqlDbType.NVarChar).Value = dto.Email;
            cm.ExecuteNonQuery();
            conn.Close();
        }

        public void XoaNhaCungCap(int ma)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_XoaNhaCungCap";
            cm.Parameters.Add("@MaNhaCungCap", SqlDbType.Int).Value = ma;
            cm.ExecuteNonQuery();
            conn.Close();
        }

        public void CapNhatNhaCungCap(NhaCungCapDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_CapNhatNhaCungCap";
            cm.Parameters.Add("@MaNhaCungCap", SqlDbType.Int).Value = dto.TenNhaCungCap;
            cm.Parameters.Add("@TenNhaCungCap", SqlDbType.NVarChar).Value = dto.TenNhaCungCap;
            cm.Parameters.Add("@SoDienThoai", SqlDbType.VarChar).Value = dto.SoDienThoai;
            cm.Parameters.Add("@DiaChi", SqlDbType.NVarChar).Value = dto.DiaChi;
            cm.Parameters.Add("@Email", SqlDbType.NVarChar).Value = dto.Email;
            cm.ExecuteNonQuery();
            conn.Close();
        }
    }
}
